{include file="../application/admin/view/common/header.html" /}

<!--main content start-->
<section class="main-content">


    <!--start page content-->

    <div class="row">

        <div class="col-md-12">
            <div class="panel panel-default collapsed">
                <div class="panel-heading">
                    <a class="btn btn-danger ui-w" onclick="delete_all()" href="javascript:void(0);"><span
                            class="ui-icon ui-icon-trash"> </span>批量删除</a>
                    <a class="btn btn-blue ui-w" onclick="start_backup(this)" href="javascript:void(0);">备份数据库</a>
                    <div class="float-m" style="display: inline-block;">
                        <label>日期范围:</label>
                        <input type="text" id="logmin" class="input-text Wdate"
                               style="width:100px;display: inline-block;">
                        -
                        <input type="text" id="logmax" class="input-text Wdate"
                               style="width:100px;display: inline-block;">
                    </div>
                    <div class="search-form" style="margin-left:10px;width: 200px; ">
                        <input type="text" class="form-control" style="width: 200px;" placeholder="输入文件名"/>
                        <button type="button" id="submit" class="btn-search"><i class="fa fa-search"></i></button>
                    </div>
                </div>
                <div class="panel-body">

                    <table class="table table-hover table-striped table-bordered" data-page-size="12"
                           data-filter=#filter>
                        <thead>
                        <tr>
                            <th>选择</th>
                            <th>序号</th>
                            <th>文件名</th>
                            <th>文件大小</th>
                            <th>备份时间</th>
                            <th>操作</th>
                        </tr>
                        </thead>
                        <tbody>


                        </tbody>
                    </table>
                    <div class="pager" id="page" style="text-align: right;"></div>
                </div>
            </div>
        </div>
    </div><!--end row-->

    <!--end page content-->


    <!-- <footer class="footer">
        <span></span>
    </footer> -->


</section>
<!--end main content-->


<!--Common plugins-->
<script src="_JS_/jquery.min.js"></script>
<script src="_JS_/jquery.base64.js"></script>
<script src="_JS_/jquery.cookie.js"></script>
<script src="_JS_/bootstrap.min.js"></script>
<script src="_JS_/jasny-bootstrap.min.js"></script>
<script src="_JS_/jquery.slimscroll.min.js"></script>
<script src="_JS_/jquery.nanoscroller.min.js"></script>
<script src="_JS_/metismenu.min.js"></script>
<script src="_JS_/float-custom.js"></script>
<script type="text/javascript" src="_JS_/layer/layer.js"></script>
<script type="text/javascript" src="_JS_/laypage/laypage.js"></script>
<script type="text/javascript" src="_JS_/laydata/laydate.js"></script>
<script type="text/javascript" src="_JS_/getMyDate.js"></script>
<!-- <script src="js/footable.all.min.js"></script> -->
<!-- Page-Level Scripts -->
<script>
    $(function () {
        $("#page-title").html('数据库备份');
        postfile(1);
    })


    function postfile(page) {
        var username = $('.search-form input').val();
        var logmin = $("#logmin").val();
        var logmax = $("#logmax").val();
        $.ajax({
            type: 'GET',
            url: "{:url('api/Param/backup')}",
            dataType: 'jsonp',
            jsonp: 'callback',
            data: {
                'page': page,
                'username': username,
                'logmin': logmin,
                'logmax': logmax
            },
            beforeSend: function () {
                var index = layer.load(1, {
                    shade: [0.1, '#333333'] //0.1透明度的白色背景
                });
            },
            success: function (msg) {
                layer.closeAll();
                $("#system_management").addClass("active").children("ul").addClass("in");
                $("#database_backup").addClass("choose");
                if (msg.code == 200) {
                    var pages = Math.ceil(msg.data.count / 10);
                    $(".table tbody").html('');
                    if (msg.data.data.length > 0) {
                        for (var i = 0; i < msg.data.data.length; i++) {
                            var time = msg.data.data[i].time;
                            ltime = getMyDate(time * 1000);

                            var html = '<tr id="' + msg.data.data[i].id + '">';
                            html += '<td><input type="checkbox" name="allcheck"></td><td>' + msg.data.data[i].id + '</td>';
                            html += '<td class="name">' + msg.data.data[i].name + '</td><td>' + msg.data.data[i].size + '</td>';
                            html += '<td>' + ltime + '</td>';
                            html += '<td class="buttons td-manage"><a class="btn table-btn " onclick="database_backup(this,' + msg.data.data[i].id + ')" href="javascript:;"' +
                                ' title="回滚数据库"><i class="fa fa-reply"></i></a><a class="btn table-btn" onclick="database_del(this,' + msg.data.data[i].id + ')" href="#" ' +
                                'title="删除"><i class="fa fa-trash-o"></i></a></td>';
                            $('.table tbody').append(html);
                        }
                    }
                    //分页
                    $(".table").attr("pages", page);
                    laypage({
                        cont: 'page',
                        pages: pages,
                        curr: page,
                        jump: function (obj, first) {
                            if (!first) { //点击跳页触发函数自身，并传递当前页：obj.curr
                                postfile(obj.curr);
                            }
                        }
                    })
                } else {
                    layer.msg('获取信息不成功!', {icon: 5, time: 2000});
                }
            }
        })
    }

    $('#submit').click(function () {
        postfile(1);
    })

    //回车键绑定搜索
    $(".search-form").keydown(function (event) {
        if (event.keyCode == 13) {
            postfile(1);
        }
    });

    /*批量删除*/
    function delete_all(obj) {
        var arrs = new Array();
        $(".table input[name='allcheck']:checked").each(function () {
            arrs.push($(this).parents('tr').attr('id'));
        });
        if (arrs.length == 0) {
            layer.msg('请选择删除项!', {icon: 5, time: 1000});
            return false;
        }
        var pages_num = $(".table").attr("pages");
        var uids = arrs.join(",");
        var name = 'all';
        var tp = 'del';
        layer.confirm('确认要删除多个吗？', function (index) {
            //此处请求后台程序，下方是成功后的前台处理……
            $.ajax({
                type: "get",
                url: "{:url('api/Param/start_backup')}",
                dataType: 'jsonp',
                jsonp: 'callback',
                data: {'tp': tp, 'ids': uids, 'name': name},
                success: function (msg) {
                    if (msg.code == 200) {
                        // $(obj).parents('tr').remove();
                        postfile(pages_num);
                        layer.msg('已删除!', {icon: 1, time: 1000});
                    }
                    if (msg.code == 1000) {
                        layer.msg('删除失败!', {icon: 5, time: 1000});
                    }
                }
            });
        });
    }

    //备份
    function start_backup(obj) {
        var page_id = $(".table").attr("pages");
        var n = 20;//上传进度
        var tp = 'backup';
        layer.confirm('您是否确定要备份数据库？', {
            btn: ['备份', '取消'] //按钮
        }, function () {
            $.ajax({
                type: 'get',
                url: "{:url('api/Param/start_backup')}",
                data: {'tp': tp},
                dataType: 'jsonp',
                jsonp: 'callback',
                success: function (msg) {
                    var index = layer.confirm();
                    layer.close(index);
                    layer.open({
                        type: 1,
                        title: '备份进度',
                        closeBtn: 0,
                        area: ['420px', '100px'], //宽高
                        content: '<div style="width:300px;height:30px;border:1px solid #dde6e9;margin-left:60px;margin-top:10px;"><span style="display:block;height:30px;width:' + n + '%;background-color:#5eb95e;text-align:right;line-height:30px;color:#ffffff;">' + n + '%</span></div>'
                    });
                    // layer.closeAll();//备份完成时关闭
                    postfile(page_id);
                }
            });
        }, function () {
            layer.closeAll();
        });

    }

    /*回滚*/
    function database_backup(obj, id) {
        var n = 50;
        var page_id = $(".table").attr("pages");
        var tp = 'restore';
        layer.confirm('您是否确定要回滚数据库？', {
            btn: ['回滚', '取消'] //按钮
        }, function () {
            $.ajax({
                type: 'get',
                url: "{:url('api/Param/start_backup')}",
                data: {'tp': tp, 'id': id},
                dataType: 'jsonp',
                jsonp: 'callback',
                success: function (msg) {
                    if (msg.code == 200) {
                        var index = layer.confirm();
                        layer.close(index);
                        layer.open({
                            type: 1,
                            title: '回滚进度',
                            closeBtn: 0,
                            area: ['420px', '100px'], //宽高
                            content: '<div style="width:300px;height:30px;border:1px solid #dde6e9;margin-left:60px;margin-top:10px;"><span style="display:block;height:30px;width:' + n + '%;background-color:#5eb95e;text-align:right;line-height:30px;color:#ffffff;">' + n + '%</span></div>'
                        });//width百分比为回滚进去百分比
                        layer.closeAll();//回滚完成时关闭
                        postfile(page_id);
                    }
                }
            }, function () {
                layer.closeAll();
            });
        })
    }

    /*删除*/
    function database_del(obj, id) {
        layer.confirm('确认要删除吗？', function (index) {
            //此处请求后台程序，下方是成功后的前台处理……
            var page_id = $(".table").attr("pages");
            var tp = 'del';
            $.ajax({
                type: 'get',
                url: "{:url('api/Param/start_backup')}",
                data: {'tp': tp, id: id},
                dataType: 'jsonp',
                jsonp: 'callback',
                success: function (msg) {
                    $(obj).parents("tr").remove();
                    layer.msg('已删除!', {icon: 1, time: 1000});
                    postfile(page_id);
                }
            });
        });
    }


    //日期范围限制
    var start = {
        elem: '#logmin',
        format: 'YYYY-MM-DD',
        min: '1970-01-01', //设定最小日期为当前日期
        max: laydate.now(), //最大日期
        istime: false,
        istoday: false,
        choose: function (datas) {
            end.min = datas; //开始日选好后，重置结束日的最小日期
            end.start = datas //将结束日的初始值设定为开始日
        }
    };

    var end = {
        elem: '#logmax',
        format: 'YYYY-MM-DD',
        min: '1970-01-01',
        max: laydate.now(),
        istime: false,
        istoday: false,
        choose: function (datas) {
            start.max = datas; //结束日选好后，充值开始日的最大日期
        }
    };
    laydate(start);
    laydate(end);


</script>
</body>
</html>